package cn.hnbjx.prepaid.controller;

import cn.hnbjx.prepaid.common.PassToken;
import cn.hnbjx.prepaid.pojo.Account;
import cn.hnbjx.prepaid.pojo.WxParameter;
import cn.hnbjx.prepaid.service.AccountService;
import cn.hnbjx.prepaid.service.WxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/api/login")
public class
LoginController {

    @Autowired
    WxService wxService;
    @Autowired
    WxParameter wxParameter;

    @Autowired
    AccountService accountService;

    /**
     * 访问 / 时，可传state参数
     *
     * @param response
     * @throws IOException
     **/
    @PassToken
    @GetMapping("/mp/auth")
    public void index(HttpServletResponse response, HttpSession httpSession, HttpServletRequest request) throws IOException {
        String appid = wxParameter.getmpAppId();
        //uri授权后重定向的回调链接地址
        String redirect_uri = "http://" + wxParameter.getDomain() + "/api/login/mp/login";
        //应用授权作用域，snsapi_base （不弹出授权页面，直接跳转，只能获取用户openid），snsapi_userinfo （弹出授权页面，可通过openid拿到昵称、性别、所在地。并且， 即使在未关注的情况下，只要用户授权，也能获取其信息 ）
        String scope = "snsapi_userinfo";
        String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid
                + "&redirect_uri=" + redirect_uri
                + "&response_type=code"
                + "&scope=" + scope
                + "&state=" + "STATE"
                + "#wechat_redirect";
//        System.out.println(url);
        System.out.println(redirect_uri);
        response.sendRedirect(url);
    }

}